package cc.rengu.igas.channel.upac.biz.impl;

import cc.rengu.igas.channel.upac.common.constant.UpacServiceConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.facade.UpacService;
import cc.rengu.igas.channel.upac.facade.request.*;
import cc.rengu.igas.channel.upac.facade.response.*;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class UpacServiceImpl implements UpacService {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<UpacTransStateQueryResponse> upacTransStatusQuery(UpacTransStateQueryRequest upacTransStateQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<UpacTransStateQueryResponse> result = new Result<>();
        UpacTransStateQueryResponse transStateQueryResponse = new UpacTransStateQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(upacTransStateQueryRequest,
                    UpacServiceConstant.UPAC_PAY_OPEN_SERVICE, UpacTransStateQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((UpacTransStateQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("交易结果通知失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                transStateQueryResponse.setRespDesc(((BizException) e).getCode());
                transStateQueryResponse.setRespDesc(e.getMessage());
            } else {
                transStateQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                transStateQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<TokenConsumeCancelResponse> upacConsumeCancel(TokenConsumeCancelRequest tokenConsumeCancelRequest) {
        return null;
    }

    @Override
    public Result<UpacConsumeRefundResponse> upacConsumeRefund(UpacConsumeRefundRequest upacConsumeRefundRequest) {
        return null;
    }

    @Override
    public Result<UpacBillQueryResponse> upacBillQuery(UpacBillQueryRequest upacBillQueryRequest) {
        return null;
    }

    @Override
    public Result<UpacBillPayResponse> upacBillPay(UpacBillPayRequest upacBillPayRequest) {
        return null;
    }

    @Override
    public Result<UpacEstablishEntrustResponse> establishEntrust(UpacEstablishEntrustRequest upacEstablishEntrustRequest) {
        return null;
    }

    @Override
    public Result<UpacUndelegationResponse> undelegation(UpacUndelegationRequest upacUndelegationRequest) {
        return null;
    }
}
